package py.com.alfa.escriba.web.rest.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

import py.com.alfa.escriba.config.EscribaApplicationContextProvider;
import py.com.alfa.escriba.config.HttpHeaderConfig;
import py.com.alfa.escriba.exception.ErrorCode;
import py.com.alfa.escriba.exception.ServiceException;
import py.com.alfa.escriba.persistence.dto.VehiculosDTO;
import py.com.alfa.escriba.persistence.dto.VehiculosinfoDTO;
import py.com.alfa.escriba.util.JSONTranslator;
import py.com.alfa.escriba.web.interceptor.BeanNames;
import py.com.alfa.escriba.web.interceptor.InvocationContext;
import py.com.alfa.escriba.web.rest.secure.VehiculosSecureRESTService;
import py.com.alfa.escriba.web.services.VehiculosService;

public class VehiculosSecureRESTServiceImpl implements VehiculosSecureRESTService{

  private ApplicationContext ctx;
  private VehiculosService dao;
  private InvocationContext ic;
  private HttpHeaderConfig config;
  private Logger logger = LoggerFactory.getLogger(getClass());

  public VehiculosSecureRESTServiceImpl() {
    this.ctx = EscribaApplicationContextProvider.getContext();
    this.dao = (VehiculosService) ctx.getBean("vehiculosService");
    this.config = (HttpHeaderConfig) ctx.getBean(BeanNames.HTTP_HEADERS_BEAN);
  }

  @Override
  public List<VehiculosDTO> listVehiculos(HttpServletRequest request) throws ServiceException { 
    try {
      ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
      List<VehiculosDTO> result = dao.listVehiculos(ic);
      return result;
    }
    catch (ServiceException e){
      logger.error(e.getMessage(), e);
      throw e;
    }
    catch (Exception e){
      logger.error(e.getMessage(), e);
      throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
    }
  }
  
  @Override
  public List<VehiculosinfoDTO> listDatosVehiculosInfo(HttpServletRequest request) throws ServiceException { 
    try {
      ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
      List<VehiculosinfoDTO> result = dao.listDatosVehiculosInfo(ic);
      return result;
    }
    catch (ServiceException e){
      logger.error(e.getMessage(), e);
      throw e;
    }
    catch (Exception e){
      logger.error(e.getMessage(), e);
      throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
    }
  }

  @Override
  public VehiculosDTO showVehiculos(Long id, HttpServletRequest request) throws ServiceException { 
    try {
      ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
      VehiculosDTO result = dao.getVehiculos(id, ic);
      return result;
    }
    catch (ServiceException e){
      logger.error(e.getMessage(), e);
      throw e;
    }
    catch (Exception e){
      logger.error(e.getMessage(), e);
      throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
    }
  }

  @Override
  public VehiculosDTO saveVehiculos(Long id, VehiculosDTO dto, HttpServletRequest request) throws ServiceException {
    try {
      ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
      VehiculosDTO result = dao.updateVehiculos(id, dto, ic);
      return result;
    }
    catch (ServiceException e){
      logger.error(e.getMessage(), e);
      throw e;
    }
    catch (Exception e){
      logger.error(e.getMessage(), e);
      throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
    }
  }

  @Override
  public VehiculosDTO createVehiculos(VehiculosDTO dto, HttpServletRequest request) throws ServiceException {
    try {
      ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
      VehiculosDTO result = dao.createVehiculos(dto, ic);
      return result;
    }
    catch (ServiceException e){
      logger.error(e.getMessage(), e);
      throw e;
    }
    catch (Exception e){
      logger.error(e.getMessage(), e);
      throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
    }
  }

  @Override
  public Response deleteVehiculos(Long id, HttpServletRequest request) throws ServiceException {
    try {
      ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
      dao.deleteVehiculos(id, ic);
      return Response.ok().build();
    }
    catch (ServiceException e){
      logger.error(e.getMessage(), e);
      throw e;
    }
    catch (Exception e){
      logger.error(e.getMessage(), e);
      throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
    }
  }

  @Override
  public List<VehiculosDTO> buscarVehiculos(VehiculosDTO dto, HttpServletRequest request) throws ServiceException {
	try {
		JSONTranslator<VehiculosDTO> trans = new JSONTranslator<VehiculosDTO>(VehiculosDTO.class);
		logger.info("VEHICULO:_{}", trans.toJSON(dto));
		ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
		List<VehiculosDTO> list = this.dao.buscarVehiculos(dto, ic);
		return list;
	}
	catch (ServiceException e){
		logger.error(e.getMessage(), e);
		throw e;
	}
	catch (Exception e){
		logger.error(e.getMessage(), e);
		throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
	}
  }

}